package com.dotstone.chipism.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.Key;
import com.dotstone.chipism.bean.MySocket;
import com.dotstone.chipism.bean.Status;
import com.dotstone.chipism.util.ConvertUtil;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.KeyManager;
import com.dotstone.chipism.util.MyDBHelper;
import com.dotstone.chipism.util.SocketManager;
import com.dotstone.chipism.util.ToastShow;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
//import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RemoteTVActivity extends BaseActivity {
	private LinearLayout mBackL;
	private TextView mStatusBar;
	private Button mPowerB;
	private TextView mTitleTv;
	private boolean power = true;
	private Button mVoiceBtn;
	private Button mCollectB;
	private Button mTvB;
	private Button mTvDownB;
	private Button mTvUpB;
	private Button mUpB;
	private Button mDownB;
	private Button mLeftB;
	private Button mRightB;
	private Button mOkB;
	private Button mSounddownB;
	private Button mSoundupB;
	private Button mReturnB;
	private Button mErrorB;
	private Button mSoundB;
	private Button mNumB;
	private Button mMenuB;
	private Button mQuitB;
	private Button mMoreB;
	private Button mBtn0;
	private Button mBtn1;
	private Button mBtn2;
	private Button mBtn3;
	private Button mBtn4;
	private Button mBtn5;
	private Button mBtn6;
	private Button mBtn7;
	private Button mBtn8;
	private Button mBtn9;
	private Button mBtnNum;
	private Vibrator vibrator;
	private MySocket socket;
	private Device device;
	private List<Key> keys = new ArrayList<Key>();
	private List<Key> moreKeys = new ArrayList<Key>();
	private List<Button> buttons = new ArrayList<Button>();
	private HashMap<Integer, String> cmdMaps = new HashMap<Integer, String>();
	private Dialog dialog;
	private Dialog mNumDialog;
	private GridView mKeysG;
	private KeyAdapter mAdapter;
	private MyDBHelper myDBHelper;
	private Status status;
	private RelativeLayout fangxiang_layout;

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;
		case R.id.power_b:
			if (power) { // 当前打开 发送关指令
				mPowerB.setBackgroundResource(R.drawable.kaiguan22x);
				if (cmdMaps.get(4000) == null || cmdMaps.get(4000) == "") {
					sendCmd(cmdMaps.get(800));
				} else {
					sendCmd(cmdMaps.get(4000));
				}
				resetButton(false);
			} else { // 当前关闭 发送开的指令
				mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
				sendCmd(cmdMaps.get(800));
				resetButton(true);
			}
			power = !power;
			if (power) {
				myDBHelper.updateStatus(device.getDeviceID(), 0, 0, 0, 0);
			} else {
				myDBHelper.updateStatus(device.getDeviceID(), 1, 0, 0, 0);
			}
			vibrator.vibrate(50);
			break;
		case R.id.voice_btn:
			ToastShow.Show(getApplicationContext(), "尚未实现");
			break;
		case R.id.collect_btn:
			// 收藏 2001
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(2001));
			break;
		case R.id.tv_btn:
			// 信源 801
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(801));
			break;
		case R.id.tv_down_btn:
			// 频道- 808
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(808));
			break;
		case R.id.tv_up_btn:
			// 频道+ 807
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(807));
			break;
		case R.id.up_btn:
			// 菜单上翻 818
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(818));
			break;
		case R.id.down_btn:
			// 菜单下翻 819
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(819));
			break;
		case R.id.left_btn:
			// 菜单左翻 820
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(820));
			break;
		case R.id.right_btn:
			// 菜单右翻 821
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(821));
			break;
		case R.id.ok_btn:
			// 菜单OK 817
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(817));
			break;
		case R.id.sound_down_btn:
			// 音量- 810
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(810));
			break;
		case R.id.sound_up_btn:
			// 音量+ 809
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(809));
			break;
		case R.id.return_btn:
			// 返回 806
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(806));
			break;
		case R.id.error_btn:
			ToastShow.Show(getApplicationContext(), "无按键信息");
			break;
		case R.id.sound_btn:
			// 静音 804
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(804));
			break;
		case R.id.num_btn:
			// 弹出数字框
			mNumDialog.show();
			break;
		case R.id.menu_btn:
			// 菜单 822
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(822));
			break;
		case R.id.quit_btn:
			// 菜单退出 823
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(823));
			break;
		case R.id.more_btn:
			// ToastShow.Show(getApplicationContext(), "尚无更多按键");
			Log.i("wmy", "更多 响应点击");
			initDialog();
			break;
		case R.id.num0_btn:
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(0));
			break;
		case R.id.num1_btn:
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(1));
			break;
		case R.id.num2_btn:
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(2));
			break;
		case R.id.num3_btn:
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(3));
			break;
		case R.id.num4_btn:
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(4));
			break;
		case R.id.num5_btn:
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(5));
			break;
		case R.id.num6_btn:
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(6));
			break;
		case R.id.num7_btn:
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(7));
			break;
		case R.id.num8_btn:
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(8));
			break;
		case R.id.num9_btn:
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(9));
			break;
		case R.id.digit_btn:
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(805));
			break;
		default:
			break;
		}
	}

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_tv;
	}

	private void initDialog() {
		// TODO Auto-generated method stub
		dialog = new Dialog(RemoteTVActivity.this, R.style.MyDialogStyle);
		dialog.setContentView(R.layout.dialog_more_key);
		Window dialogWindow = dialog.getWindow();
		mKeysG = (GridView) dialog.findViewById(R.id.key_gv);
		mAdapter = new KeyAdapter();
		mKeysG.setAdapter(mAdapter);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
		dialog.show();
	}

	private void initNumDialog() {
		mNumDialog = new Dialog(RemoteTVActivity.this, R.style.MyDialogStyle);
		mNumDialog.setContentView(R.layout.dialog_num);
		Window dialogWindow = mNumDialog.getWindow();
		mBtn0 = (Button) mNumDialog.findViewById(R.id.num0_btn);
		mBtn1 = (Button) mNumDialog.findViewById(R.id.num1_btn);
		mBtn2 = (Button) mNumDialog.findViewById(R.id.num2_btn);
		mBtn3 = (Button) mNumDialog.findViewById(R.id.num3_btn);
		mBtn4 = (Button) mNumDialog.findViewById(R.id.num4_btn);
		mBtn5 = (Button) mNumDialog.findViewById(R.id.num5_btn);
		mBtn6 = (Button) mNumDialog.findViewById(R.id.num6_btn);
		mBtn7 = (Button) mNumDialog.findViewById(R.id.num7_btn);
		mBtn8 = (Button) mNumDialog.findViewById(R.id.num8_btn);
		mBtn9 = (Button) mNumDialog.findViewById(R.id.num9_btn);
		mBtnNum = (Button) mNumDialog.findViewById(R.id.digit_btn);
		mBtn0.setOnClickListener(this);
		mBtn1.setOnClickListener(this);
		mBtn2.setOnClickListener(this);
		mBtn3.setOnClickListener(this);
		mBtn4.setOnClickListener(this);
		mBtn5.setOnClickListener(this);
		mBtn6.setOnClickListener(this);
		mBtn7.setOnClickListener(this);
		mBtn8.setOnClickListener(this);
		mBtn9.setOnClickListener(this);
		mBtnNum.setOnClickListener(this);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		mBackL = $(R.id.back_layout);
		mStatusBar = $(R.id.tv_statusBar_mian);
		mTitleTv = $(R.id.title_tv);
		mPowerB = $(R.id.power_b);
		mVoiceBtn = $(R.id.voice_btn);
		mCollectB = $(R.id.collect_btn);
		mTvB = $(R.id.tv_btn);
		mTvDownB = $(R.id.tv_down_btn);
		mTvUpB = $(R.id.tv_up_btn);
		mOkB = $(R.id.ok_btn);
		mUpB = $(R.id.up_btn);
		mDownB = $(R.id.down_btn);
		mLeftB = $(R.id.left_btn);
		mRightB = $(R.id.right_btn);
		mSounddownB = $(R.id.sound_down_btn);
		mSoundupB = $(R.id.sound_up_btn);
		mReturnB = $(R.id.return_btn);
		mErrorB = $(R.id.error_btn);
		mSoundB = $(R.id.sound_btn);
		mNumB = $(R.id.num_btn);
		mMenuB = $(R.id.menu_btn);
		mQuitB = $(R.id.quit_btn);
		mMoreB = $(R.id.more_btn);
		fangxiang_layout = $(R.id.fangxiang_layout);
		// 初始化数字键位弹出框
		initNumDialog();
		boolVersion();
		vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		int position = getIntent().getIntExtra("position", -1);
		if (position != -1) {
			device = DeviceManager.getInstance().getRemotes().get(position);
		} else {
			String id = getIntent().getStringExtra("id");
			device = DeviceManager.getInstance().getDeviceById(id);
		}
		myDBHelper = new MyDBHelper(getApplicationContext());
		status = myDBHelper.getStatusById(device.getDeviceID());
		mTitleTv.setText(device.getDeviceName());
		socket = SocketManager.getInstance().sMap.get(device.getMainDeviceId());
		if (socket == null) {
			ToastShow.Show(getApplicationContext(), "该设备无绑定网关，无法被控制！");
			finish();
		}
		initKey();
		if (status != null) {
			initStatus();
		}
		mBackL.setOnClickListener(this);
		mPowerB.setOnClickListener(this);
		mVoiceBtn.setOnClickListener(this);
		mCollectB.setOnClickListener(this);
		mTvB.setOnClickListener(this);
		mTvDownB.setOnClickListener(this);
		mTvUpB.setOnClickListener(this);
		mOkB.setOnClickListener(this);
		mUpB.setOnClickListener(this);
		mDownB.setOnClickListener(this);
		mLeftB.setOnClickListener(this);
		mRightB.setOnClickListener(this);
		mSounddownB.setOnClickListener(this);
		mSoundupB.setOnClickListener(this);
		mReturnB.setOnClickListener(this);
		mErrorB.setOnClickListener(this);
		mSoundB.setOnClickListener(this);
		mNumB.setOnClickListener(this);
		mMenuB.setOnClickListener(this);
		mQuitB.setOnClickListener(this);
		mMoreB.setOnClickListener(this);
	}

	private void initStatus() {
		if (status.isPower()) {
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			power = true;
		} else {
			mPowerB.setBackgroundResource(R.drawable.kaiguan22x);
			power = false;
		}
		resetButton(power);
	}

	private void setBg(int id, boolean e) {
		if (e) {

			switch (id) {
			case R.id.collect_btn:
				mCollectB.setClickable(true);
				mCollectB.setBackgroundResource(R.drawable.selector_collect);
				break;
			case R.id.tv_btn:
				mTvB.setClickable(true);
				mTvB.setBackgroundResource(R.drawable.selector_tv_av);
				break;
			case R.id.tv_up_btn:
				mTvUpB.setClickable(true);
				mTvUpB.setBackgroundResource(R.drawable.selector_tv_up);
				break;
			case R.id.tv_down_btn:
				mTvDownB.setClickable(true);
				mTvDownB.setBackgroundResource(R.drawable.selector_tv_down);
				break;
			case R.id.sound_btn:
				mSoundB.setClickable(true);
				mSoundB.setBackgroundResource(R.drawable.shengyin12x);
				break;
			case R.id.num_btn:
				mNumB.setClickable(true);
				mNumB.setBackgroundResource(R.drawable.selector_num);
				break;
			case R.id.quit_btn:
				mQuitB.setClickable(true);
				mQuitB.setBackgroundResource(R.drawable.selector_quit);
				break;
			case R.id.error_btn:
				mErrorB.setClickable(true);
				mErrorB.setBackgroundResource(R.drawable.selector_error);
				break;
			case R.id.menu_btn:
				mMenuB.setClickable(true);
				mMenuB.setBackgroundResource(R.drawable.selector_menu);
				break;
			case R.id.ok_btn:
				mOkB.setClickable(true);
				mOkB.setBackgroundResource(R.drawable.selector_ok);
				break;
			case R.id.up_btn:
				mUpB.setClickable(true);
				mUpB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.down_btn:
				mDownB.setClickable(true);
				mDownB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.left_btn:
				mLeftB.setClickable(true);
				mLeftB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.right_btn:
				mRightB.setClickable(true);
				mRightB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.sound_up_btn:
				mSoundupB.setClickable(true);
				mSoundupB.setBackgroundResource(R.drawable.selector_sound_up);
				break;
			case R.id.sound_down_btn:
				mSounddownB.setClickable(true);
				mSounddownB.setBackgroundResource(R.drawable.selector_sound_down);
				break;
			case R.id.return_btn:
				mReturnB.setClickable(true);
				mReturnB.setBackgroundResource(R.drawable.selector_return);
				break;
			case R.id.num0_btn:
				mBtn0.setClickable(true);
				mBtn0.setBackgroundResource(R.drawable.selector_num0);
				break;
			case R.id.num1_btn:
				mBtn1.setClickable(true);
				mBtn1.setBackgroundResource(R.drawable.selector_num1);
				break;
			case R.id.num2_btn:
				mBtn2.setClickable(true);
				mBtn2.setBackgroundResource(R.drawable.selector_num2);
				break;
			case R.id.num3_btn:
				mBtn3.setClickable(true);
				mBtn3.setBackgroundResource(R.drawable.selector_num3);
				break;
			case R.id.num4_btn:
				mBtn4.setClickable(true);
				mBtn4.setBackgroundResource(R.drawable.selector_num4);
				break;
			case R.id.num5_btn:
				mBtn5.setClickable(true);
				mBtn5.setBackgroundResource(R.drawable.selector_num5);
				break;
			case R.id.num6_btn:
				mBtn6.setClickable(true);
				mBtn6.setBackgroundResource(R.drawable.selector_num6);
				break;
			case R.id.num7_btn:
				mBtn7.setClickable(true);
				mBtn7.setBackgroundResource(R.drawable.selector_num7);
				break;
			case R.id.num8_btn:
				mBtn8.setClickable(true);
				mBtn8.setBackgroundResource(R.drawable.selector_num8);
				break;
			case R.id.num9_btn:
				mBtn9.setClickable(true);
				mBtn9.setBackgroundResource(R.drawable.selector_num9);
				break;
			default:
				break;
			}
		} else {
			switch (id) {
			case R.id.collect_btn:
				mCollectB.setClickable(false);
				mCollectB.setBackgroundResource(R.drawable.xxing12x);
				break;
			case R.id.tv_btn:
				mTvB.setClickable(false);
				mTvB.setBackgroundResource(R.drawable.tvav1x);
				break;
			case R.id.tv_up_btn:
				mTvUpB.setClickable(false);
				mTvUpB.setBackgroundResource(R.drawable.tvjia2x);
				break;
			case R.id.tv_down_btn:
				mTvDownB.setClickable(false);
				mTvDownB.setBackgroundResource(R.drawable.tvjian2x);
				break;
			case R.id.sound_btn:
				mSoundB.setClickable(false);
				mSoundB.setBackgroundResource(R.drawable.shengyin12x);
				break;
			case R.id.num_btn:
				mNumB.setClickable(false);
				mNumB.setBackgroundResource(R.drawable.a1232x);
				break;
			case R.id.quit_btn:
				mQuitB.setClickable(false);
				mQuitB.setBackgroundResource(R.drawable.tuichu1x);
				break;
			case R.id.error_btn:
				mErrorB.setClickable(false);
				mErrorB.setBackgroundResource(R.drawable.gantan12x);
				break;
			case R.id.menu_btn:
				mMenuB.setClickable(false);
				mMenuB.setBackgroundResource(R.drawable.caidan1x);
				break;
			case R.id.ok_btn:
				mOkB.setClickable(false);
				mOkB.setBackgroundResource(R.drawable.okoo12x);
				break;
			case R.id.up_btn:
				mUpB.setClickable(false);
				mUpB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.down_btn:
				mDownB.setClickable(false);
				mDownB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.left_btn:
				mLeftB.setClickable(false);
				mLeftB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.right_btn:
				mRightB.setClickable(false);
				mRightB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.sound_up_btn:
				mSoundupB.setClickable(false);
				mSoundupB.setBackgroundResource(R.drawable.yinjia12x);
				break;
			case R.id.sound_down_btn:
				mSounddownB.setClickable(false);
				mSounddownB.setBackgroundResource(R.drawable.yinjian12x);
				break;
			case R.id.return_btn:
				mReturnB.setClickable(false);
				mReturnB.setBackgroundResource(R.drawable.fanhui12x);
				break;
			case R.id.num0_btn:
				mBtn0.setClickable(false);
				mBtn0.setBackgroundResource(R.drawable.a02x);
				break;
			case R.id.num1_btn:
				mBtn1.setClickable(false);
				mBtn1.setBackgroundResource(R.drawable.a12x);
				break;
			case R.id.num2_btn:
				mBtn2.setClickable(false);
				mBtn2.setBackgroundResource(R.drawable.a22x);
				break;
			case R.id.num3_btn:
				mBtn3.setClickable(false);
				mBtn3.setBackgroundResource(R.drawable.a32x);
				break;
			case R.id.num4_btn:
				mBtn4.setClickable(false);
				mBtn4.setBackgroundResource(R.drawable.a42x);
				break;
			case R.id.num5_btn:
				mBtn5.setClickable(false);
				mBtn5.setBackgroundResource(R.drawable.a52x);
				break;
			case R.id.num6_btn:
				mBtn6.setClickable(false);
				mBtn6.setBackgroundResource(R.drawable.a62x);
				break;
			case R.id.num7_btn:
				mBtn7.setClickable(false);
				mBtn7.setBackgroundResource(R.drawable.a72x);
				break;
			case R.id.num8_btn:
				mBtn8.setClickable(false);
				mBtn8.setBackgroundResource(R.drawable.a82x);
				break;
			case R.id.num9_btn:
				mBtn9.setClickable(false);
				mBtn9.setBackgroundResource(R.drawable.a92x);
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			// getWindow().addFlags(
			// WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mStatusBar.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			mStatusBar.setLayoutParams(layoutParams1);
			mStatusBar.setVisibility(View.VISIBLE);
		} else {
			mStatusBar.setVisibility(View.GONE);
		}
	}

	private void initKey() {
		buttons.clear();
		buttons.add(mCollectB);
		buttons.add(mTvB);
		buttons.add(mTvUpB);
		buttons.add(mTvDownB);
		buttons.add(mMenuB);
		buttons.add(mUpB);
		buttons.add(mDownB);
		buttons.add(mLeftB);
		buttons.add(mRightB);
		buttons.add(mOkB);
		buttons.add(mSoundupB);
		buttons.add(mSounddownB);
		buttons.add(mReturnB);
		buttons.add(mSoundB);
//		buttons.add(mNumB);
		buttons.add(mErrorB);
		buttons.add(mQuitB);
		buttons.add(mBtn0);
		buttons.add(mBtn1);
		buttons.add(mBtn2);
		buttons.add(mBtn3);
		buttons.add(mBtn4);
		buttons.add(mBtn5);
		buttons.add(mBtn6);
		buttons.add(mBtn7);
		buttons.add(mBtn8);
		buttons.add(mBtn9);
		moreKeys.clear();
		keys = device.getKeys();
		for (int i = 0; i < keys.size(); i++) {
			String cmd = ConvertUtil.getInstance().convertInfraredCmd(keys.get(i).getCodeValue());
			int p = keys.get(i).getKeyNum();
			cmdMaps.put(p, cmd);
		}
		for (int i = 0; i < buttons.size(); i++) {
			// Log.i("wmy",
			// "buttons.get(i).getTag() = "
			// + Integer.parseInt(buttons.get(i).getTag()
			// .toString()));
			String cmd = cmdMaps.get(Integer.parseInt(buttons.get(i).getTag().toString()));
			if (cmd == null) {
				setBg(buttons.get(i).getId(), false);
			} else {
				setBg(buttons.get(i).getId(), true);
			}
		}
		for (int i = 0; i < keys.size(); i++) {
			// 如果按键不是温度加和温度减 进行判断
			if (keys.get(i).getKeyNum() != 811 && keys.get(i).getKeyNum() != 812 && keys.get(i).getKeyNum() != 800
					&& keys.get(i).getKeyNum() != 4000) {
				boolean e = false;
				for (Button btn : buttons) {
					int tag = Integer.parseInt(btn.getTag().toString());
					if (tag == keys.get(i).getKeyNum()) {
						e = true;
						// moreKeys.add(keys.get(i));
						break;
					}
				}
				if (!e) {
					boolean e1 = false;
					for (int n = 0; n < moreKeys.size(); n++) {
						if (moreKeys.get(n).getKeyNum() == keys.get(i).getKeyNum()) {
							e1 = true;
						}
					}
					if (!e1) {
						// Log.i("wmy", "moreKeys添加 = " +
						// keys.get(i).getKeyNum()
						// + " " + i);
						moreKeys.add(keys.get(i));
					}
				}
			}
		}
		// Log.i("wmy", "moreKeys.size() = " + moreKeys.size());
		// for (int i = 0; i < moreKeys.size(); i++) {
		//// Log.i("wmy", "moreKeys.get(i).getKeyNum() = "
		//// + moreKeys.get(i).getKeyNum());
		// }
		if (moreKeys.size() > 0) {
			mMoreB.setBackgroundResource(R.drawable.selector_more1);
			// initDialog();
			mMoreB.setClickable(true);
		} else {
			mMoreB.setBackgroundResource(R.drawable.ddd12x);
			// Log.i("wmy", "无更多按键");
			mMoreB.setClickable(false);
		}
	}

	private void sendCmd(String cmd) {
		if (socket.isLocalSuccess()) {
			socket.sendMqttData(cmd);
		} else {
			SocketManager.getInstance().sendRemoteData(socket.getMac(), DeviceManager.getInstance().getmWlanId(), cmd);
		}
	}

	private void resetButton(boolean e) {
		if (e) {
			initKey();
			mMoreB.setClickable(true);
			fangxiang_layout.setBackgroundResource(R.drawable.daanniu1x);
		} else {
			for (int i = 0; i < buttons.size(); i++) {
				setBg(buttons.get(i).getId(), false);
			}
			mMoreB.setClickable(false);
			Log.i("wmy", "设置为不可点击");
			mMoreB.setBackgroundResource(R.drawable.ddd12x);
			fangxiang_layout.setBackgroundResource(R.drawable.daanniu2x);
		}
	}

	class KeyAdapter extends BaseAdapter {
		HoldView holdView;
		private int imageResId;
		private int stringResId;

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return moreKeys.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return moreKeys.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (v == null) {
				v = LayoutInflater.from(RemoteTVActivity.this).inflate(R.layout.item_more_key, null);
				holdView = new HoldView();
				holdView.btn = (Button) v.findViewById(R.id.key_btn);
				v.setTag(holdView);
			} else {
				holdView = (HoldView) v.getTag();
			}
			final com.dotstone.chipism.bean.Key k = moreKeys.get(position);
			holdView.btn.setTag(k.getKeyNum());
			int content = KeyManager.getInstance().getContentTypeByKeyNum(k.getKeyNum());
			if (content == 0) {
				imageResId = KeyManager.getInstance().getDrawableResId(k.getKeyNum());
				holdView.btn.setBackgroundResource(imageResId);
			} else {
				stringResId = KeyManager.getInstance().getStringResId(k.getKeyNum());
				holdView.btn.setText(stringResId);
			}
			holdView.btn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					// Log.i("wmy", "点击了" + k.getKeyNum());
					int tag = Integer.parseInt(v.getTag().toString());
					sendCmd(cmdMaps.get(tag));
					vibrator.vibrate(50);
				}
			});

			return v;
		}
	}

	class HoldView {
		private Button btn;
	}

}
